// *************************************************************************************************
//
// MK3D Engine
// --------------------------------------
// Copyright (C) 2007-2011 Zeb (mailto: zebbey@gmail.com)
//
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
//
// *************************************************************************************************

#ifndef __H_MK_ERROR__
#define __H_MK_ERROR__

#include <assert.h>
#include <DxErr.h>
#pragma comment(lib, "DxErr.lib")

#include "mk_engine.h"
#include "mk_log.h"

#undef ASSERT
#ifdef _DEBUG
#define ASSERT(exp) assert(exp)
#else
#define ASSERT(exp) (void(0))
#endif

#ifndef SAFE_RELEASE
#define SAFE_RELEASE(n)\
	do\
	{\
		if (n)\
		{\
			(n)->Release();\
			(n) = NULL;\
		}\
	} while (0)
#endif

#ifndef SAFE_DELETE
#define SAFE_DELETE(n)\
	do\
		{\
		if (n)\
				{\
				delete (n);\
				(n) = NULL;\
				}\
		} while (0)
#endif

#ifndef SAFE_DELETE_ARRAY
#define SAFE_DELETE_ARRAY(n)\
	do\
	{\
		if (n)\
		{\
			delete [](n);\
			(n) = NULL;\
		}\
	} while (0)
#endif

#ifndef MK_PROCESS_ERROR
#define MK_PROCESS_ERROR(n)\
	do\
	{\
		if (!(n))\
		{\
			GetEngine()->GetLog()->Write(MKLT_ERROR, "file: [%s] line: [%d]", __FILE__, __LINE__); \
			goto Exit0;\
		}\
	} while (0);
#endif

#ifndef MK_PROCESS_COM_ERROR
#define MK_PROCESS_COM_ERROR(hr)\
	do\
	{\
		if (FAILED(hr))\
		{\
			GetEngine()->GetLog()->Write(MKLT_ERROR, "desc: [%s] file: [%s] line: [%d]", DXGetErrorDescription(hr), __FILE__, __LINE__); \
			goto Exit0;\
		}\
	} while (0);
#endif

#ifndef MK_PROCESS_ERROR_NO_GOTO
#define MK_PROCESS_ERROR_NO_GOTO(n)\
	do\
	{\
		if (!(n))\
		{\
			GetEngine()->GetLog()->Write(MKLT_ERROR, "file: [%s] line: [%d]", __FILE__, __LINE__); \
		}\
	} while (0);
#endif

#ifndef MK_PROCESS_COM_ERROR_NO_GOTO
#define MK_PROCESS_COM_ERROR_NO_GOTO(hr)\
	do\
	{\
		if (FAILED(hr))\
			{\
				GetEngine()->GetLog()->Write(MKLT_ERROR, "desc: [%s] file: [%s] line: [%d]", DXGetErrorDescription(hr), __FILE__, __LINE__); \
			}\
	} while (0);
#endif

#endif /* __H_MK_ERROR__ */
